package quq.missq.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import quq.missq.BaseActivity;
import quq.missq.Constants;
import quq.missq.R;
import quq.missq.beans.Album;
import quq.missq.utils.HttpRequest;
import quq.missq.utils.ImageUploadAsyncTask;
import quq.missq.utils.ImageUtil;
import quq.missq.utils.UserTools;
/**
 * Created by River on 8/13/15.
 */
public class AddAlbumActivity extends BaseActivity implements View.OnClickListener{


    private static final int SELECT_PIC = 0;

    private ImageView mBack;
    private ImageView mConfirm;
    private ImageView mAlbumCover;
    private EditText mAlbumName;
    private String mPath = "";
    private ProgressDialog dialog;
    private String title = "创建相册";
    private TextView mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_album_activity);
        title = getIntent().getExtras().getString("title");
        initView();
        initListener();
    }

    private void initView(){
        mBack = (ImageView)findViewById(R.id.back);
        mConfirm = (ImageView)findViewById(R.id.confirm);
        mAlbumCover = (ImageView)findViewById(R.id.albumCover);
        mAlbumName = (EditText)findViewById(R.id.albumName);
        mTitle = (TextView)findViewById(R.id.title);
        mTitle.setText(title);
    }

    private void initListener(){
        mBack.setOnClickListener(this);
        mConfirm.setOnClickListener(this);
        mAlbumCover.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == SELECT_PIC) {
            mPath = data.getStringExtra(SelectPicActivity.KEY_PHOTO_PATH);
            mAlbumCover.setImageBitmap(ImageUtil.readFile(mPath));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.confirm:
                sendRquest();
                break;
            case R.id.albumCover:
                Intent intent = new Intent(this, SelectPicActivity.class);
                intent.putExtra(SelectPicActivity.KEY_CUT_X,4);
                intent.putExtra(SelectPicActivity.KEY_CUT_Y,2);
                startActivityForResult(intent, SELECT_PIC);
                break;
        }
    }

    private void sendRquest(){
        if("".equals(mPath)){
            showToast("请上传封面");
            return;
        }
        String albumName = mAlbumName.getText().toString();
        if("".equals(albumName)){
            showToast("请输入相册名称");
            return;
        }
        dialog = ProgressDialog.show(this, "提示", "上传中...", false, false);
        Map<String,String> params = new HashMap<>();
        params.put("name",albumName);
        params.put("acc_token",UserTools.getUser(this).getAcc_token());
        Map<String,File> files = new HashMap<>();
        files.put("pic",new File(mPath));
        ImageUploadAsyncTask imageUploadAsyncTask = new ImageUploadAsyncTask(params,files, Constants.ADD_ALBUM_URL, new HttpRequest.HttpRequestListener() {
            @Override
            public void httpError() {
                if(dialog != null){dialog.dismiss();}
                showToast("上传失败，请重试");
            }

            @Override
            public void httpSuccess(String msg) {
                if(dialog != null){dialog.dismiss();}
                try {
                    JSONObject json = new JSONObject(msg);
                    int code = json.getInt("code");
                    if(code >= 0){
                        JSONObject obj = json.getJSONObject("data");
                        Intent intent = new Intent();
                        Bundle bundle = new Bundle();
                        Album album = new Album();
                        album.setId(obj.getInt("id"));
                        album.setCover(obj.getString("cover"));
                        album.setName(obj.getString("name"));
                        bundle.putSerializable("album",album);
                        intent.putExtras(bundle);
                        setResult(RESULT_OK,intent);
                        AddAlbumActivity.this.finish();
                    }else{
                        showToast(json.getString("message"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },"file");
        imageUploadAsyncTask.execute();

    }

}
